package com.woniuxy.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @Author: wanghao
 * @Date: 2019/9/23 9:25
 */
@Configuration
public class CorsConfig {

        /*
         * @Override public void addCorsMappings(CorsRegistry registry) { registry.
         * addMapping("/**"). allowedOrigins("*"). allowedMethods("GET", "HEAD", "POST",
         * "PUT", "DELETE", "OPTIONS")
         * .allowCredentials(true).maxAge(3600).allowedHeaders("*"); }
         */

    @Bean
    public CorsFilter corsFilter() { // 本质都是自定义http请求头
          UrlBasedCorsConfigurationSource source = new
          UrlBasedCorsConfigurationSource(); CorsConfiguration corsConfiguration = new
          CorsConfiguration(); corsConfiguration.addAllowedOrigin("*");
         corsConfiguration.addAllowedHeader("*");
          corsConfiguration.addAllowedMethod("*");
          source.registerCorsConfiguration("/**", corsConfiguration); return new
          CorsFilter(source); }

    }

